package br.usp.icmc.biomac.client.view.lixo;

import java.util.ArrayList;
import java.util.List;

import br.usp.icmc.biomac.shared.CSVData;
import br.usp.icmc.biomac.shared.CSVSerie;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.TreeViewModel;

public class TableTreeImpl extends Composite implements TableTree {

	private static TableTreeUiBinder uiBinder = GWT
			.create(TableTreeUiBinder.class);
	
	@SuppressWarnings("unused")
	private Presenter presenter;

	private static class CustomTreeModel implements TreeViewModel {

		private final List<CSVData> cvsData;

		/**
		 * This selection model is shared across all leaf nodes. A selection
		 * model can also be shared across all nodes in the tree, or each set of
		 * child nodes can have its own instance. This gives you flexibility to
		 * determine how nodes are selected.
		 */
		@SuppressWarnings("unused")
		private final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();

		@SuppressWarnings("unused")
		public CustomTreeModel() {
			// Create a database of information.
			cvsData = new ArrayList<CSVData>();

			// Add csvData.
			{
				CSVData cvs = new CSVData();
				cvsData.add(cvs);
				cvs.addCSVSerie(new CSVSerie("PAtiente 0"));
				cvs.addCSVSerie(new CSVSerie("PAtiente 1"));
				cvs.addCSVSerie(new CSVSerie("PAtiente 2"));
				cvs.addCSVSerie(new CSVSerie("PAtiente 3"));
			}

		}
		public CustomTreeModel(List<CSVData> arrayList) {
			cvsData = arrayList;
		}

		/**
		 * Get the {@link NodeInfo} that provides the children of the specified
		 * value.
		 */
		public <T> NodeInfo<?> getNodeInfo(T value) {
			if (value == null) {
				// LEVEL 0.
				// We passed null as the root value. Return the cvsData.

				// Create a data provider that contains the list of cvsData.
				ListDataProvider<CSVData> dataProvider = new ListDataProvider<CSVData>(
						cvsData);

				// Create a cell to display a composer.
				Cell<CSVData> cell = new AbstractCell<CSVData>() {
					@Override
					public void render(Context context, CSVData value,
							SafeHtmlBuilder sb) {
						if (value != null) {
							sb.appendEscaped(value.getPatientName());//
						}
					}
				};

				// Return a node info that pairs the data provider and the cell.
				return new DefaultNodeInfo<CSVData>(dataProvider, cell);
			} else if (value instanceof CSVData) {
				// LEVEL 1.
				// We want the children of the composer. Return the playlists.
				ListDataProvider<CSVSerie> dataProvider = new ListDataProvider<CSVSerie>(((CSVData) value).getSeries());
				Cell<CSVSerie> cell = new AbstractCell<CSVSerie>() {
					@Override
					public void render(Context context, CSVSerie value,
							SafeHtmlBuilder sb) {
						if (value != null) {
							sb.appendEscaped(value.getThumbnailURL());
						}
					}
				};
				return new DefaultNodeInfo<CSVSerie>(dataProvider, cell);
			}
			return null;
		}

		/**
		 * Check if the specified value represents a leaf node. Leaf nodes
		 * cannot be opened.
		 */
		public boolean isLeaf(Object value) {
			// The leaf nodes are the songs, which are Strings.
			if (value instanceof CSVSerie) {
				return true;
			}
			return false;
		}

	}

	@UiField(provided=true) CellTree cellTree;

	interface TableTreeUiBinder extends UiBinder<Widget, TableTreeImpl> {
	}

	public TableTreeImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	public void setTableData(List<CSVData> csvData){
		TreeViewModel model = new CustomTreeModel(csvData);
		cellTree = new CellTree(model, null);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

}
